package cn.com.duiba.millionaire.center.spider.spdierjob;

import cn.com.duiba.millionaire.center.spider.common.PageConsumer;
import cn.com.duiba.millionaire.center.spider.entity.SpiderAppInfoEntity;
import cn.com.duiba.millionaire.center.spider.service.SpiderAppInfoService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.List;

/**
 * http://www.pc6.com/
 * <p>
 * Created by sunchangji on 2018/6/7.
 */
@Component
public class Pc6SpiderJob {
    @Autowired
    private SpiderAppInfoService spiderAppInfoService;

    private static final String DEST_PRIX = "http://www.pc6.com/az/";
    private static final String PRIF = "http://www.pc6.com/android/";
    private static final String URL_HOST = "http://www.pc6.com";

    public PageConsumer pageConsumer = page -> {
        String url = page.getUrl().get();
        if (url.contains(PRIF)) {
            Html html = page.getHtml();
            List<String> targetUrls = getTargetUrls(html,url);
            if (CollectionUtils.isNotEmpty(targetUrls)) {
                page.addTargetRequests(targetUrls);
            }

        } else if (url.contains(DEST_PRIX)) {
            Html html = page.getHtml();
            String appName = html.xpath("//dd[@id='dinfo']//h1//text()").get();
            if(StringUtils.isNotEmpty(appName)){
                String category = html.xpath("//dd[@id='dinfo']//p[@class='base']//i//a//text()").get();
                String updateTime = html.xpath("//dd[@id='dinfo']//p[@class='base']//i[4]//text()").get();
                String desc = html.xpath("//dd[@id='soft-info']//p[1]//text()").get();
                SpiderAppInfoEntity entity = new SpiderAppInfoEntity();
                entity.setAppName(appName);
                entity.setCategory(category);
                if(StringUtils.isNotEmpty(updateTime)){
                    updateTime = updateTime.replace("更新：","").replaceAll("/","-");
                    entity.setUpdatedDate(updateTime);
                }
                entity.setEmail(url);
                if(StringUtils.isNotEmpty(desc)){
                    entity.setDescription(desc.trim());
                }
                spiderAppInfoService.inserAppInfo(entity);
            }

        }

    };

    private List<String> getTargetUrls(Html html,String nowUrl) {

        List<String> targetUrls = html.xpath("//dl[@id='listCont']//dd//p").links().regex("/az/[0-9]+\\.html").all();
        if (CollectionUtils.isEmpty(targetUrls)) {
            return null;
        }

        List<String> targetUrlList = new ArrayList<>(targetUrls.size());
        String nextUrl = getNextPageUrl(nowUrl);
        targetUrlList.add(nextUrl);
        for (String url : targetUrls) {
            String urlTar = URL_HOST + url;
            targetUrlList.add(urlTar);
        }

        return targetUrlList;
    }


    private String getNextPageUrl(String url) {
        String preUrl = url.substring(0,url.lastIndexOf("_")+1);
        String index = url.substring(url.lastIndexOf("_")+1,url.lastIndexOf("."));
        Integer nextIndex = Integer.valueOf(index)+1;
        return preUrl+nextIndex+".html";
    }
}
